package com.example.vj.robotface_test.util;

import android.graphics.Point;
import android.text.TextUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.Iterator;

/**
 * Created by vj on 17-9-26.
 */

public class ParseResult {

    static public FaceRect[] paraseResult(String json)
    {
        FaceRect[] rect = null;
        if (TextUtils.isEmpty(json))
        {
            return null;
        }
        try{
            JSONTokener tokener = new JSONTokener(json);
            JSONObject joResult = new JSONObject(tokener);
            int ret = joResult.optInt("ret");
            if(ret!=0)
            {
                return null;
            }
            JSONArray items = joResult.getJSONArray("face");

            rect = new FaceRect[items.length()];
            for(int i=0;i<items.length();i++)
            {
                JSONObject position = items.getJSONObject(i).getJSONObject("position");

                rect[i] = new FaceRect();
                rect[i].bound.left = position.getInt("left");
                rect[i].bound.top = position.getInt("top");
                rect[i].bound.right = position.getInt("right");
                rect[i].bound.bottom = position.getInt("bottom");

                try {
                    JSONObject landmark = items.getJSONObject(i).getJSONObject("landmark");
                    int keyPoint = landmark.length();
                    rect[i].point = new Point[keyPoint];
                    Iterator it = landmark.keys();
                    int point = 0;
                    while(it.hasNext()&&point<keyPoint)     //获取焦点位置
                    {
                        String key = (String)it.next();
                        JSONObject postion = landmark.getJSONObject(key);
                        rect[i].point[point] = new Point(postion.getInt("x"),postion.getInt("y"));
                        point++;
                    }
                }catch(JSONException e)

                {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rect;
    }
}
